package com.qf.controller;

import com.qf.entity.TbEvlBasic;
import com.qf.entity.TbGoods;
import com.qf.service.TbEvlBasicService;
import com.qf.service.TbGoodsService;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class EvlController {


    private String[] temp;
    @Autowired
    private TbEvlBasicService evlService;

    @Autowired
    private TbGoodsService goodsService;

    @RequestMapping("evl")
    public String evl(String goodsId, Model model) {
        //查询点击对象
        TbGoods byId = goodsService.findById(goodsId);
        //存储查找对象
        model.addAttribute("goods", byId);
        //查找对应的明细
        List<TbEvlBasic> stp1 = evlService.findByEvlId(goodsId, "1");
        List<TbEvlBasic> stp2 = evlService.findByEvlId(goodsId, "2");
        List<TbEvlBasic> stp3 = evlService.findByEvlId(goodsId, "3");
//        存放明细
        model.addAttribute("stp1", stp1);
        model.addAttribute("stp2", stp2);
        model.addAttribute("stp3", stp3);
        return "evl";
    }

    @RequestMapping("doEvl/{goodsId}")
    @ResponseBody
    public Result doEvl(@PathVariable String goodsId, @RequestBody String[] entity) {
        temp = entity;
        Result<Integer> integerResult = new Result<>();
        if (goodsId != null) {
            TbGoods byId = goodsService.findById(goodsId);
            Integer goodsCost = byId.getGoodsCost();
            for (Integer integer : evlService.findGod(goodsId, entity)) {
                goodsCost = goodsCost + integer;
            }
            integerResult.setData(goodsCost);
            integerResult.setSuccess(true);
            integerResult.setMsg("成功");
        } else {
            integerResult.setData(-1);
            integerResult.setSuccess(false);
            integerResult.setMsg("没有传入id");
        }
        return integerResult;
    }

    @RequestMapping("price")
    public String doPrice(String goodsId,Model model) {
        List<String> details = evlService.findDetails(temp);
        model.addAttribute("details",details);
        TbGoods goods = goodsService.findById(goodsId);
        model.addAttribute("goods",goods);
        return "price";
    }

}
